package com.softer.chengxin.train.service.impl;

import com.softer.chengxin.common.domin.*;
import com.softer.chengxin.common.enums.ResultEnum;
import com.softer.chengxin.train.dao.TrainDao;
import com.softer.chengxin.train.entity.Train;
import com.softer.chengxin.train.service.TrainService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Gong Zhiyang
 * @date 2018/10/13
 */
@Service
public class TrainServiceImpl implements TrainService {
    @Autowired
    private TrainDao trainDao;

    /**
     * 添加培训记录
     *
     * @param train 培训记录
     * @return 添加结果
     */
    @Override
    public CommonResult add(Train train) {

        //校验培训情况
//        CommonResult commonResult = checkTrain(train);
//        if (ResultEnum.FAIL.getCode() == commonResult.getData()) {
//            return commonResult;
//        }
        int num = trainDao.add(train);
        if (num > Integer.parseInt(Common.ZERO)) {
            return new CommonResult(ResultEnum.SUCCESS.getCode(), "添加成功");
        }
        return new CommonResult(ResultEnum.FAIL.getCode(), "添加失败");
    }

    /**
     * 校验培训情况
     *
     * @param train 培训实体
     * @return 校验结果
     */
    public CommonResult checkTrain(Train train) {
        if (StringUtils.isNotBlank(train.getCertificate())) {
            String certificate = trainDao.checkPersonTrain(train.getCertificate());
            if (StringUtils.isNotBlank(certificate)) {
                return new CommonResult(ResultEnum.FAIL.getCode(), "该证书编号已经审核通过，请确认后添加");
            }
        }
        if (Common.ONE.equals(train.getSubjectType())) {
            //如果是项目经理培训，必须填写证书编码
            if (StringUtils.isBlank(train.getCertificate())) {
                return new CommonResult(ResultEnum.FAIL.getCode(), "请添加证书编号");
            }
            //通过人员ID查询人员名字
            String personName = trainDao.getPersonName(train.getPersonId());
            //验证证书编号和培训人员
            String managerTrain = trainDao.checkProjectManagerTrain(personName.trim(), train.getCertificate().trim());
            if (StringUtils.isBlank(managerTrain)) {
                return new CommonResult(ResultEnum.FAIL.getCode(), "请添加正确的人员和证书编号");
            }
        }
        return new CommonResult(ResultEnum.SUCCESS.getCode(), "成功");
    }


    /**
     * 提交培训记录
     *
     * @param id 培训ID
     * @return 结果
     */
    @Override
    public CommonResult submit(Integer id) {
        int num = trainDao.submit(id);
        if (num > Integer.parseInt(Common.ZERO)) {
            return new CommonResult(ResultEnum.SUCCESS.getCode(), "提交成功");
        }
        return new CommonResult(ResultEnum.FAIL.getCode(), "提交失败");
    }

    /**
     * 编辑培训记录
     *
     * @param train 培训记录
     * @return 结果
     */
    @Override
    public CommonResult edit(Train train) {
//        //校验培训情况
//        CommonResult commonResult = checkTrain(train);
//        if (ResultEnum.FAIL.getCode() == commonResult.getData()) {
//            return commonResult;
//        }
        int num = trainDao.edit(train);
        if (num > Integer.parseInt(Common.ZERO)) {
            return new CommonResult(ResultEnum.SUCCESS.getCode(), "修改成功");
        }
        return new CommonResult(ResultEnum.FAIL.getCode(), "修改失败");
    }

    /**
     * 刪除培训记录
     *
     * @param id 培训ID
     * @return 结果
     */
    @Override
    public CommonResult delete(Integer id) {
        int num = trainDao.delete(id);
        if (num > Integer.parseInt(Common.ZERO)) {
            return new CommonResult(ResultEnum.SUCCESS.getCode(), "删除成功");
        }
        return new CommonResult(ResultEnum.FAIL.getCode(), "删除失败");
    }

    /**
     * 查询培训列表
     *
     * @param page  分页实体
     * @param name  人员姓名
     * @param train 查询实体
     * @return 列表
     */
    @Override
    public ManagerPageResult queryTrainList(Page page, String name, Train train) {
        ManagerPageResult mr = new ManagerPageResult();
        JsonResult jr = new JsonResult();
        int num = trainDao.queryTrainNum(name, train);
        if (num > Integer.parseInt(Common.ZERO)) {
            List<Train> list = trainDao.queryTrainList(page, name, train);
            return setResult(mr, jr, num, list);
        }
        return setResult(mr, jr, Integer.parseInt(Common.ZERO), new ArrayList());
    }

    /**
     * 查询具体人员培训列表
     *
     * @param page 页面实体
     * @param id   培训ID
     * @return 列表
     */
    @Override
    public ManagerPageResult queryPersonTrainList(Page page, Integer id) {
        ManagerPageResult mr = new ManagerPageResult();
        JsonResult jr = new JsonResult();
        int num = trainDao.queryPersonTrainNum(id);
        if (num > Integer.parseInt(Common.ZERO)) {
            List<Train> list = trainDao.queryPersonTrainList(page, id);
            return setResult(mr, jr, num, list);
        }
        return setResult(mr, jr, Integer.parseInt(Common.ZERO), new ArrayList());
    }

    /**
     * 查询培训详情
     *
     * @param id 培训id
     * @return 详情
     */
    @Override
    public Train detail(Integer id) {
        return trainDao.detail(id);
    }

    /**
     * 根据人员ID获取项目名称
     *
     * @param id 人员ID
     * @return 项目名称
     */
    @Override
    public String getProjectName(String id) {
        String projectName = trainDao.getProjectName(id);
        if (StringUtils.isBlank(projectName)) {
            return "无";
        }
        return projectName;
    }

    /**
     * 设置返回参数
     *
     * @param mpr  ManagerPageResult
     * @param jr   JsonResult
     * @param num  数量
     * @param list 集合列表
     */
    private ManagerPageResult setResult(ManagerPageResult mpr, JsonResult jr, int num, List list) {
        mpr.setCount(num);
        jr.setDs(list);
        mpr.setJson(jr);
        return mpr;
    }
}
